package com.fick.struts.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.fick.hibernate.dao.MagecontentDAO;
import com.fick.hibernate.dao.MageeditionDAO;
import com.fick.hibernate.dao.MagephaseDAO;
import com.fick.hibernate.dao.MagezineDAO;
import com.fick.hibernate.model.Magecontent;
import com.fick.hibernate.model.Mageedition;
import com.fick.util.General;
import com.fick.util.UpPic;

public class ManageMagecontentAction extends DispatchAction {

	MagezineDAO magezineDAO = new MagezineDAO();
	MagephaseDAO phaseDAO = new MagephaseDAO();
	MageeditionDAO editionDAO = new MageeditionDAO();
	MagecontentDAO contentDAO = new MagecontentDAO();

	public ActionForward addMagecon(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		UpPic uppic = new UpPic();
		// 配置上传环境
		uppic.setObjectPath(request.getSession().getServletContext().getRealPath("/UpFile/") + "/");
		uppic.setSize(10000 * 1024);
		uppic.setSuffix(".rar.zip.gif.jpg.png.jpge.html.htm.swf.doc.xls");
		uppic.setSourceFile(request);
		String[] saObjectFile = uppic.getObjectFileName();
		String picName = "";
		// 判断是否上传了图片
		if (saObjectFile[0] != null) {
			picName = "UpFile/" + saObjectFile[0];
		}

		String mageid = General.convertNullToEmpty(uppic.getFieldValue("mageid"));
		String phaseid = General.convertNullToEmpty(uppic.getFieldValue("phaseid"));
		String editionid = General.convertNullToEmpty(uppic.getFieldValue("editionid"));

		String newName = General.convertNullToEmpty(uppic.getFieldValue("newName"));
		String content = General.convertNullToEmpty(uppic.getFieldValue("content"));

		try {
			Magecontent magecontent = new Magecontent();
			magecontent.setTitle(newName);
			magecontent.setContent(content);
			magecontent.setAdddate(new java.util.Date());

			magecontent.setMageid(Integer.valueOf(mageid));
			magecontent.setPhaseid(Integer.valueOf(phaseid));
			if (editionid != null && !editionid.trim().equals("")) {
				Mageedition edition = editionDAO.findById(Integer.valueOf(editionid));
				magecontent.setMageedition(edition);
			}
			magecontent.setPicpath(picName);
			contentDAO.save(magecontent);
			request.setAttribute("message", "添加成功");
			return getList(mapping, form, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// delAllCon
	public ActionForward delAllCon(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String str = request.getParameter("str");

		Magecontent content = new Magecontent();
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				content = contentDAO.findById(id);
				contentDAO.delete(content);
			}

			request.setAttribute("message", "全部删除成功");
			return mapping.findForward("showList");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public ActionForward delMagecon(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String contentid = request.getParameter("contentid");

		try {
			Magecontent magecontent = contentDAO.findById(Integer.valueOf(contentid));
			contentDAO.delete(magecontent);
			request.setAttribute("message", "删除成功");
			return mapping.findForward("showList");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public ActionForward getList(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		try {

			List magezineList = magezineDAO.findAll();
			List phaseList = phaseDAO.findAll();
			List editionList = editionDAO.findAll();

			request.setAttribute("magezineList", magezineList);
			request.setAttribute("phaseList", phaseList);
			request.setAttribute("editionList", editionList);

			return mapping.findForward("addcon");

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public ActionForward upMagecon(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		UpPic uppic = new UpPic();
		// 配置上传环境
		uppic.setObjectPath(request.getSession().getServletContext().getRealPath("/UpFile/") + "/");
		uppic.setSize(10000 * 1024);
		uppic.setSuffix(".rar.zip.gif.jpg.png.jpge.html.htm.swf.doc.xls");
		uppic.setSourceFile(request);
		String[] saObjectFile = uppic.getObjectFileName();
		int iCount = uppic.getCount();

		String picName = "";
		// 判断是否上传了图片
		if (saObjectFile[0] != null) {
			picName = "UpFile/" + saObjectFile[0];
		}
		String sObjectPath = uppic.getObjectPath();

		String mageid = General.convertNullToEmpty(uppic.getFieldValue("mageid"));
		String phaseid = General.convertNullToEmpty(uppic.getFieldValue("phaseid"));
		String editionid = General.convertNullToEmpty(uppic.getFieldValue("editionid"));

		String contentid = General.convertNullToEmpty(uppic.getFieldValue("contentid"));
		String newName = General.convertNullToEmpty(uppic.getFieldValue("newName"));
		String content = General.convertNullToEmpty(uppic.getFieldValue("content"));

		try {
			Magecontent magecontent = contentDAO.findById(Integer.valueOf(contentid));

			magecontent.setTitle(newName);
			magecontent.setContent(content);
			magecontent.setAdddate(new java.util.Date());

			if (mageid != null && !mageid.trim().equals("")) {
				magecontent.setMageid(Integer.valueOf(mageid));
			}
			if (phaseid != null && !phaseid.trim().equals("")) {
				magecontent.setPhaseid(Integer.valueOf(phaseid));
			}

			if (editionid != null && !editionid.trim().equals("")) {
				Mageedition edition = editionDAO.findById(Integer.valueOf(editionid));
				magecontent.setMageedition(edition);
			}
			magecontent.setPicpath(picName);
			contentDAO.update(magecontent);
			request.setAttribute("message", "修改更新成功");
			return mapping.findForward("showList");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}
